package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @BelongsProject: sky-take-out
 * @BelongsPackage: com.sky.service.impl
 * @Author: song
 * @CreateTime: 2024-01-02  14:27
 * @Description: 套餐相关操作代码具体实现
 * @Version: 1.0
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    /*
     * @description:新增套餐，同时需要保存套餐和菜品的关联关系代码实现
     * @author: song
     * @date: 2024/1/2 14:32
     * @param: [setmealDTO]
     * @return: com.sky.result.Result
     **/
    @Override
    @Transactional
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        // 向套餐表中插入数据
        setmealMapper.insert(setmeal);
        // 获得生成id
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        setmealDishMapper.insertBatch(setmealDishes);
    }
    /*
     * @description:套餐分页查询
     * @author: song
     * @date: 2024/1/2 14:57
     * @param: [setmealPageQueryDTO]
     * @return: com.sky.result.PageResult
     **/
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        int pageNum = setmealPageQueryDTO.getPage();
        int pageSize = setmealPageQueryDTO.getPageSize();
        PageHelper.startPage(pageNum,pageSize);
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());

    }
    /*
     * @description:批量删除套餐
     * @author: song
     * @date: 2024/1/2 15:13
     * @param: [ids]
     * @return: void
     **/
    @Override
    public void deleteBatch(List<Long> ids) {
        ids.forEach(id ->{
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE)
                // 起售中的商品不允许被删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        });
        ids.forEach(setmealId ->{
            // 根据套餐id删除套餐
            setmealMapper.deleteById(setmealId);
            // 根据套餐id删除套餐和菜品的关联关系
            setmealDishMapper.deleteBySetmealId(setmealId);
        });
    }
    /*
     * @description:根据id查询套餐信息
     * @author: song
     * @date: 2024/1/2 15:39
     * @param: [id]
     * @return: com.sky.vo.SetmealVO
     **/
    @Override
    public SetmealVO getByIdWithDish(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        List<SetmealDish> setmealDishes = setmealDishMapper
                .getBySetmealId(id);//TODO
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }
    /*
     * @description:修改套餐
     * @author: song
     * @date: 2024/1/2 15:45
     * @param:
     * @return:
     **/
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        // 修改套餐表，执行update
        setmealMapper.update(setmeal);
        // 删除套餐和菜品的关联关系，操作setmeal_dish表，执行delete
        Long setmealId = setmealDTO.getId();
        setmealDishMapper.deleteBySetmealId(setmealId);

        // 重新插入套餐和菜品的关联关系，操作setmeal_dish表，执行insert
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        setmealDishMapper.insertBatch(setmealDishes);
    }
    /*
     * @description:起售停售套餐
     * @author: song
     * @date: 2024/1/2 16:16
     * @param: [status, id]
     * @return: void
     **/
    @Override
    public void startOrStop(Integer status, Long id) {
        if (status == StatusConstant.ENABLE){
            List<Dish> dishList = dishMapper.getBySetmealId(id);
            if (dishList.size()>0 && dishList != null){
                dishList.forEach(dish -> {
                    if (StatusConstant.DISABLE == dish.getStatus()){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.update(setmeal);
    }
    /**
     * @description:条件查询
     * @author: song 
     * @date: 2024/1/10 10:12
     * @param: [setmeal]
     * @return: java.util.List<com.sky.entity.Setmeal>
     **/
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }
    /**
     * @description:根据id查询菜品选项
     * @author: song 
     * @date: 2024/1/10 10:13
     * @param: [id]
     * @return: java.util.List<com.sky.vo.DishItemVO>
     **/
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
